package com.olympia.myapp.sound;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

import com.olympia.myapp.R;

public class MyService extends Service {

	private NotificationManager nM;
	MediaPlayer sBegin;

	MediaPlayer sKD;

	MediaPlayer sVCNV;
	MediaPlayer sVCNVBegin;

	MediaPlayer sTT;
	MediaPlayer sTTBegin;
	MediaPlayer sTT30;

	MediaPlayer sVD;

	MediaPlayer sCorrect1;
	MediaPlayer sCorrect2;
	MediaPlayer sUnCorrect1;
	MediaPlayer sUnCorrect2;

	MediaPlayer sClick;

	private final IBinder binder = new LocalBinder();

	public class LocalBinder extends Binder {
		public MyService getLocal() {
			sBegin = MediaPlayer.create(getApplicationContext(), R.raw.begin);

			sKD = MediaPlayer.create(getApplicationContext(), R.raw.khoidong);

			sVCNV = MediaPlayer.create(getApplicationContext(), R.raw.vcnv);
			sVCNVBegin = MediaPlayer.create(getApplicationContext(),
					R.raw.vcnv_begin);

			sTT = MediaPlayer.create(getApplicationContext(), R.raw.tangtoc);
			sTTBegin = MediaPlayer.create(getApplicationContext(),
					R.raw.ttbegin);
			sTT30 = MediaPlayer.create(getApplicationContext(), R.raw.tt30s);

			sVD = MediaPlayer.create(getApplicationContext(), R.raw.vedich);

			sCorrect1 = MediaPlayer.create(getApplicationContext(),
					R.raw.correct1);
			sCorrect2 = MediaPlayer.create(getApplicationContext(),
					R.raw.correct2);

			sUnCorrect1 = MediaPlayer.create(getApplicationContext(),
					R.raw.notcorrect1);
			sUnCorrect2 = MediaPlayer.create(getApplicationContext(),
					R.raw.notcorrect2);

			sClick = MediaPlayer.create(getApplicationContext(), R.raw.click);

			return MyService.this;
		}
	}

	@Override
	public void onCreate() {
		nM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}

	@Override
	public void onDestroy() {
		nM.cancel(R.string.note_title);
	}

	/**
	 * chơi file begin
	 */
	public void playBegin() {
		if (sBegin != null) {
			sBegin.setLooping(true);
			sBegin.start();
		}
	}

	public void pauseBegin() {
		if (sBegin != null)
			sBegin.pause();
	}

	// public void stopBegin() {
	// if (sBegin != null)
	// sBegin.pause();
	// }

	public void playKD() {
		if (sKD != null)
			sKD.start();
	}

	public void pauseKD() {
		if (sKD != null)
			sKD.pause();
	}

	public void playVCNV() {
		if (sVCNV != null)
			sVCNV.start();
	}

	// public int getTimeVCNV() {
	// return sVCNV.getVideoWidth();
	// }

	// public void stopVCNV() {
	// if (sVCNV != null)
	// sVCNV.pause();
	// }

	public void playVCNVBegin() {
		if (sVCNVBegin != null)
			sVCNVBegin.start();
	}

	// public void stopVCNVBegin() {
	// if (sVCNVBegin != null)
	// sVCNVBegin.pause();
	// }

	public void playTT() {
		if (sTT != null)
			sTT.start();
	}

	// public void stopTT() {
	// if (sTT != null)
	// sTT.pause();
	// }

	public void playTTBegin() {
		if (sTTBegin != null)
			sTTBegin.start();
	}

	// public void stopTTBegin() {
	// if (sTTBegin != null)
	// sTTBegin.pause();
	// }

	public void playTT30() {
		if (sTT30 != null)
			sTT30.start();
	}

	// public void stopTT30() {
	// if (sTT30 != null)
	// sTT30.pause();
	// }

	public void playVD() {
		if (sVD != null)
			sVD.start();
	}

	// public void stopVD() {
	// if (sVD != null)
	// sVD.pause();
	// }

	public void playCorrect1() {
		if (sCorrect1 != null)
			sCorrect1.start();
	}

	// public void stopCorrect1() {
	// if (sCorrect1 != null)
	// sCorrect1.pause();
	// }

	public void playCorrect2() {
		if (sCorrect2 != null)
			sCorrect2.start();
	}

	// public void stopCorrect2() {
	// if (sCorrect2 != null)
	// sCorrect2.pause();
	// }

	public void playUnCorrect1() {
		if (sUnCorrect1 != null)
			sUnCorrect1.start();
	}

	// public void stopUnCorrect1() {
	// if (sUnCorrect1 != null)
	// sUnCorrect1.pause();
	// }

	public void playUnCorrect2() {
		if (sUnCorrect2 != null)
			sUnCorrect2.start();
	}

	// public void stopUnCorrect2() {
	// if (sUnCorrect2 != null)
	// sUnCorrect2.pause();
	// }

	public void playClick() {
		if (sClick != null)
			sClick.start();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}

	public void clearAll() {
		if (sBegin != null) {
			sBegin.reset();
			sBegin.release();
		}

		if (sKD != null) {
			sKD.reset();
			sKD.release();
		}
		if (sVCNV != null) {
			sVCNV.reset();
			sVCNV.release();
		}
		if (sVCNVBegin != null) {
			sVCNVBegin.reset();
			sVCNVBegin.release();
		}

		if (sTT != null) {
			sTT.reset();
			sTT.release();
		}
		if (sTT30 != null) {
			sTT30.reset();
			sTT30.release();
		}
		if (sTTBegin != null) {
			sTTBegin.reset();
			sTTBegin.release();
		}
		if (sVD != null) {
			sVD.reset();
			sVD.release();
		}

		if (sCorrect1 != null) {
			sCorrect1.reset();
			sCorrect1.release();
		}
		if (sCorrect2 != null) {
			sCorrect2.reset();
			sCorrect2.release();
		}

		if (sUnCorrect1 != null) {
			sUnCorrect1.reset();
			sUnCorrect1.release();
		}

		if (sUnCorrect2 != null) {
			sUnCorrect2.reset();
			sUnCorrect2.release();
		}

		if (sClick != null) {
			sClick.reset();
			sClick.release();
		}
	}
}
